﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditInsurance : AdminPageBase
    {
        private InsuranceInfo _insuranceInfo = new InsuranceInfo();
        private readonly InsuranceController _insuranceController = new InsuranceController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["InsuranceId"]))
                _insuranceInfo = _insuranceController.GetInsurance(int.Parse(Request.QueryString["InsuranceId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadAdminUser();
            LoadPartner();

            if (_insuranceInfo.InsuranceId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListInsurance.aspx?xml=Insurance");
        }

        private void SaveData()
        {
            _insuranceInfo.InsuranceName = txtInsuranceName.Text;
            _insuranceInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            _insuranceInfo.InsuranceLevel = txtInsuranceLevel.Text;
            _insuranceInfo.InsuranceBegin = Convert.ToDateTime(dteInsuranceBegin.Value);
            _insuranceInfo.SortDescription = txtSortDescription.Text;
            _insuranceInfo.DetailDescription = txtContent.Text;
            _insuranceInfo.Status = int.Parse(ddlStatus.SelectedValue);
            if (_insuranceInfo.InsuranceId == -1)
            {
                _insuranceInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _insuranceInfo.CreatedDate = DateTime.Now;
                _insuranceController.InsertInsurance(_insuranceInfo);
            }
            else
            {
                _insuranceInfo.UpdatedBy = UserId;
                _insuranceInfo.UpdatedDate = DateTime.Now;
                _insuranceController.UpdateInsurance(_insuranceInfo);
            }
            Response.Redirect("/Pages/ListInsurance.aspx?xml=Insurance");
        }

        private void RenderForm()
        {
            txtInsuranceName.Text = _insuranceInfo.InsuranceName;
            txtInsuranceLevel.Text = _insuranceInfo.InsuranceLevel;
            ddlPartner.SelectedValue = _insuranceInfo.PartnerId.ToString();
            dteInsuranceBegin.Value = _insuranceInfo.InsuranceBegin.ToString("dd/MM/yyyy");
            txtSortDescription.Text = _insuranceInfo.SortDescription;
            txtContent.Text = _insuranceInfo.DetailDescription;
            ddlStatus.SelectedValue = _insuranceInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _insuranceInfo.CreatedBy.ToString();
        }

        private void LoadPartner()
        {
            ddlPartner.DataSource = new PartnerController().ListPartner()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.PartnerName).ToList();
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn đối tác ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}